package com.aidong.couponsRule.biz.impl;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aidong.coupons.entity.MstCoupons;
import com.aidong.couponsRule.biz.TrnCouponsRuleBiz;
import com.aidong.couponsRule.dao.TrnCouponsRuleDao;
import com.aidong.couponsRule.entity.TrnCouponsRule;
import com.aidong.util.RandomUtil;

@Service("trnCouponsRuleBiz")
public class TrnCouponsRuleImpl implements TrnCouponsRuleBiz {

	private TrnCouponsRuleDao trnCouponsRuleDao;

	@Autowired
	public void setTrnCouponsRuleDao(TrnCouponsRuleDao trnCouponsRuleDao) {
		this.trnCouponsRuleDao = trnCouponsRuleDao;
	}

	@Override
	public Map<String, Object> getCouponsRule(Map<String, Object> map) {
		TrnCouponsRule couponsRule = trnCouponsRuleDao.getCouponsRule(map);
		MstCoupons mstCoupons = new MstCoupons();
		int result = trnCouponsRuleDao.updateCouponsRule(couponsRule);
		String beginTime = "";
		String endTime = "";
		int num = 0;
		if (result > 0) {
			mstCoupons.setDelFlg(0);
			mstCoupons.setCouponsPrice(couponsRule.getPrice());
			mstCoupons.setCreateUser("admin");
			mstCoupons.setUpdateUser("admin");
			mstCoupons.setStatus(1);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			beginTime = sdf.format(couponsRule.getBeginTime());
			endTime = sdf.format(couponsRule.getEndTime());
			mstCoupons.setValidDateBegin(couponsRule.getBeginTime());
			mstCoupons.setValidDateEnd(couponsRule.getEndTime());
			mstCoupons.setCouponsNum(RandomUtil.shortUUID(8));
			trnCouponsRuleDao.addCouponsRule(mstCoupons);
			num = 1;
		}

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("beginTime", beginTime);
		resultMap.put("num", num);
		resultMap.put("endTime", endTime);
		resultMap.put("price", mstCoupons.getCouponsPrice());
		resultMap.put("tickNum", mstCoupons.getCouponsNum());
		return resultMap;
	}

	public int addCouponsRule(MstCoupons mstCoupons) {
		return trnCouponsRuleDao.addCouponsRule(mstCoupons);
	}

	public int getCouponsCou(Map<String, Object> map) {
		return trnCouponsRuleDao.getCouponsCou(map);
	}
}
